package org.jeecg.modules.scm.common.util;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringConversion {

    /**
     * 转驼峰pattern
     * */
    private static Pattern linePattern = Pattern.compile("_(\\w)");


    /**
     *下划线转驼峰
     */
    public static String lineToHump(String str) {
        str = str.toLowerCase();
        Matcher matcher = linePattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    /*
    * 字符串添加%，以供模糊查询
    * */
    public static String changeForFuzzy(String str){
        String res="";
        for(char c:str.toCharArray()){
            res  = res + '%' + c;
        }
        return res;
    }
    /**
     * 获取对象字段值
     *
     */
    public static<T> List<String> getObjectProperty(List<T> obj,String property){
        if(obj==null){
            return null;
        }
        Pattern objPattern = Pattern.compile("("+lineToHump(property)+"=)(.*?)(,|\\))");
        List<String> list = new ArrayList();
        for(T ele:obj){
            Matcher m = objPattern.matcher(ele.toString());
            if(m.find()){
                list.add(m.group(2));
            }
        }
        return list;
    }
    public static<T> String getObjectProperty(T obj,String property){
        Pattern objPattern = Pattern.compile("("+lineToHump(property)+"=)(.*?)(,|\\))");
        Matcher m = objPattern.matcher(obj.toString());
        if(m.find()){
            return m.group(2);
        }
        return null;
    }



}
